<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加APP版本' : '编辑APP版本'"
      :visible.sync="addEditIsDialog_c"
      :close-on-click-modal="false"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="140px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本名：" prop="versionName">
              <el-input
                v-model="ruleForm.versionName"
                placeholder="请输入版本名"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本号：" prop="versionCode">
              <el-input-number
                v-model="ruleForm.versionCode"
                placeholder="请输入版本名"
                :precision="0"
                :step="1"
                :max="9999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="APP版本：" prop="versionUrl">
              <UploadApp
                @setFile="setFile"
                :fileUrl="ruleForm.versionUrl"
              ></UploadApp>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本内容：" prop="versionContent">
              <el-input
                v-model="ruleForm.versionContent"
                placeholder="请输入版本内容"
                clearable
                maxlength="200"
                type="textarea"
                :rows="2"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="是否强制更新：" prop="isForceUpdate">
              <el-select
                v-model="ruleForm.isForceUpdate"
                placeholder="请选择是否强制更新"
                clearable
                :disabled="seeType"
              >
                <el-option label="否" value="0"></el-option>
                <el-option label="是" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getSysVersion, saveSysVersion } from "@/api/system";
import UploadApp from "@/components/UploadApp.vue";

export default {
  components: {
    UploadApp,
  },
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        versionName: "",
        versionCode: "",
        versionUrl: "",
        versionContent: "",
        isForceUpdate: "",
      },
      rules: {
        // 表单规则
        versionName: [
          { required: true, message: "请输入版本名", trigger: "blur" },
        ],
        versionCode: [
          { required: true, message: "请输入版本号", trigger: "blur" },
        ],
        versionUrl: [
          { required: true, message: "请上传APP版本", trigger: "change" },
        ],
        versionContent: [
          { required: true, message: "请输入版本内容", trigger: "blur" },
        ],
        isForceUpdate: [
          { required: true, message: "请选择是否强制更新", trigger: "change" },
        ],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getSysVersion({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.isForceUpdate =
            res.data.isForceUpdate != undefined
              ? res.data.isForceUpdate.toString()
              : "";
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveSysVersion(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 上传文件
    setFile(url) {
      this.ruleForm.versionUrl = url;
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
